local qiejian = fk.CreateSkill {
  name = "wk_heg__qiejian",
  tags = {Skill.MainPlace},
}

Fk:loadTranslationTable{
  ["wk_heg__qiejian"] = "切谏",
  [":wk_heg__qiejian"] = "主将技，其他角色失去最后的手牌后，你可以与其各摸一张牌，然后你交换主副将并弃置你区域内的一张牌。",

  ["#wk_heg__qiejian-choose"] = "切谏：请选择一张牌",
  ["#wk_heg__qiejian-draw"] = "切谏：你可与 %dest 各摸一张牌",
}

local HG = require "packages.new_heg_diy_my.util"

qiejian:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(qiejian.name) then return false end
    if player.general:startsWith("ld__lord") and #player:getCardIds("ej") == 0 then return false end
    for _, move in ipairs(data) do
      if move.from and not move.from.dead then
        if move.from:isKongcheng() and move.from ~= player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local targets = {}
    local room = player.room
    for _, move in ipairs(data) do
      if move.from then
        if move.from:isKongcheng() and move.from ~= player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              table.insertIfNeed(targets, move.from)
            end
          end
        end
      end
    end
    room:sortByAction(targets)
    for _, _p in ipairs(targets) do
      if not player:hasSkill(qiejian.name) then break end
      -- if player.general:startsWith("ld__lord") and #player:getCardIds("ej") == 0 then break end
      -- local card = room:askToCards(player,{
      --   min_num = 0,
      --   max_num = 1,
      --   include_equip = true,
      --   skill_name = qiejian.name,
      --   pattern = ".|.|.|equip,judge|.|.",
      --   prompt = "#wk_heg__qiejian-choose",
      --   cancelable = true,
      -- })
      if _p:isAlive() then
        event:setCostData(self, {tos = {_p}})
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = event:getCostData(self).tos[1]
    return player.room:askToSkillInvoke(player, {skill_name = qiejian.name, prompt = "#wk_heg__qiejian-draw::" .. to.id})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local targets = {player, to}
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      p:drawCards(1, qiejian.name)
    end
    HG.SwapMainAndDeputy(room, player)
    if #player:getCardIds("hej") > 0 then
      local card = room:askToCards(player,{
        min_num = 0,
        max_num = 1,
        include_equip = true,
        skill_name = qiejian.name,
        pattern = ".|.|.|.|.|.",
        prompt = "#wk_heg__qiejian-choose",
        cancelable = false,
      })
      room:throwCard(card, qiejian.name, player, player)
    end
  end,
})

return qiejian